package com.bw.wxy.mvp.presenter;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.PhoneUtils;
import com.bw.wxy.common.App;
import com.bw.wxy.common.SpUtils;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.base.prsenter.BasePresenter;
import com.bw.wxy.mvp.contract.SplashContract;
import com.bw.wxy.mvp.model.entity.TokenEntity;

import java.util.HashMap;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class SplashPresenter extends BasePresenter<SplashContract.ISplashModel, SplashContract.ISplashView> {
    @Inject
    public SplashPresenter(SplashContract.ISplashModel mModel, SplashContract.ISplashView mView) {
        super(mModel, mView);
    }

    public void requestToken() {
//        Log.i("wxy", "requestToken: "+11);
        HashMap<String, Object> map = new HashMap<>();
        if (ActivityCompat.checkSelfPermission(App.getInstance().getApplicationContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        String imei = PhoneUtils.getIMEI();
        map.put("imie",imei);
        mModel.requestToken(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onNext);
//        Log.i("wxy", "requestToken: "+222);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
//        Log.i("wxy", "进入onNext"+22);
        TokenEntity entity= (TokenEntity) baseEntity;
//        Log.i("wxy", "entity.status: "+entity.status);
        if (entity.status.equals("200")){
//            Log.i("wxy", "status==200"+11);
            SpUtils.saveData("token",entity.getValues().token);
            mView.refresh();
        }
    }
}

